package com.calciominoBeta.Util;

import java.io.File;
import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.StrictMode;
import android.widget.ImageView;
import android.widget.Toast;

import com.calciomio.app.R;
import com.calciominoBeta.Entity.InfoOj;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiscCache;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.UsingFreqLimitedMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.utils.StorageUtils;
import com.telpoo.frame.object.BaseObject;
import com.telpoo.frame.utils.JsonSupport;
import com.telpoo.frame.utils.ViewUtils;

public class Util {
	public static Typeface tfTextView1;
	private static InterstitialAd interstitial1;
	private AdView adView;
	protected static ImageLoader imageLoader = ImageLoader.getInstance();
	protected static DisplayImageOptions options;

	public static boolean isConnectingToInternet(Context _context) {
		ConnectivityManager connectivity = (ConnectivityManager) _context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (connectivity != null) {
			NetworkInfo[] info = connectivity.getAllNetworkInfo();
			if (info != null)
				for (int i = 0; i < info.length; i++)
					if (info[i].getState() == NetworkInfo.State.CONNECTED) {
						return true;
					}
		}
		return false;
	}

	public static void checkinternet(Context mcContext) {
		final ConnectivityManager conMgr = (ConnectivityManager) mcContext
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		final NetworkInfo activeNetwork = conMgr.getActiveNetworkInfo();
		if (activeNetwork != null && activeNetwork.isConnected()) {
			// notify user you are online
		} else {
			Toast.makeText(mcContext,
					mcContext.getResources().getString(R.string.network),
					Toast.LENGTH_LONG).show();
		}
	}
	public static boolean checkinternet1(Context mcContext) {
		final ConnectivityManager conMgr = (ConnectivityManager) mcContext
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		final NetworkInfo activeNetwork = conMgr.getActiveNetworkInfo();
		if (activeNetwork != null && activeNetwork.isConnected()) {
			// notify user you are online
			return true;
		} else {
			Toast.makeText(mcContext,
					mcContext.getResources().getString(R.string.network),
					Toast.LENGTH_LONG).show();
			return false;
		}
	}
	public static void showToast(final Context context, final String msg) {
		try {
			((Activity) context).runOnUiThread(new Runnable() {

				@Override
				public void run() {
					Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
				}
			});
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static final InfoOj parseJsonAccueil(JSONObject object) {

		BaseObject oj;
		try {
			oj = JsonSupport.jsonObject2BaseOj(object);
			InfoOj oj1 = new InfoOj();
			oj1.setParams(oj.getParams());
			return oj1;

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

	@SuppressLint("NewApi")
	public static void CheckNewWork() {
		StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
				.permitAll().build();
		StrictMode.setThreadPolicy(policy);
	}

	public static void gotoActivity(Activity activity, Class<?> cls, Bundle b) {
		Intent i = new Intent(activity, cls);
		i.putExtras(b);
		activity.startActivity(i);
	}

	public static void config(Activity activity) {
		File cacheDir = StorageUtils.getOwnCacheDirectory(activity,
				"Calciomino/imageCache");
		options = new DisplayImageOptions.Builder()
				.showImageOnLoading(R.drawable.ic_launcher)
				.showImageForEmptyUri(R.drawable.ic_launcher)
				.showImageOnFail(R.drawable.ic_launcher).cacheInMemory(true)
				.cacheOnDisc(true).build();
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
				activity)
				.threadPoolSize(3)
				.threadPriority(Thread.NORM_PRIORITY - 2)
				.memoryCache(new UsingFreqLimitedMemoryCache(2000000))
				.discCache(new UnlimitedDiscCache(cacheDir))
				.memoryCacheSize(1500000)
				// 1.5 Mb

				.denyCacheImageMultipleSizesInMemory()
				.discCache(new UnlimitedDiscCache(cacheDir))
				.discCacheFileNameGenerator(new Md5FileNameGenerator()).build();

		imageLoader.init(config);
	}

	public static ArrayList<InfoOj> convertOj(ArrayList<BaseObject> ojs) {
		ArrayList<InfoOj> info = new ArrayList<InfoOj>();

		for (BaseObject oj : ojs) {

			InfoOj oj1 = new InfoOj();
			oj1.setParams(oj.getParams());
			info.add(oj1);

		}

		return info;
	}

	public static Typeface getTypeFont(Context paramContext) {
		if (tfTextView1 == null)
			tfTextView1 = Typeface.createFromAsset(paramContext.getAssets(),
					"fonts/fontawesome-webfont.ttf");
		return tfTextView1;
	}

	public static Typeface getTypeFontTitle(Context paramContext) {
		if (tfTextView1 == null)
			tfTextView1 = Typeface.createFromAsset(paramContext.getAssets(),
					"fonts/DIN 1451 Std Engschrift.ttf");
		return tfTextView1;
	}

	public static Typeface getTypeFontTitleDetail(Context paramContext) {
		if (tfTextView1 == null)
			tfTextView1 = Typeface.createFromAsset(paramContext.getAssets(),
					"fonts/DINEngschriftStd.otf");
		return tfTextView1;
	}

	public static void hilightBtn(ImageView[] img) {

		ViewUtils.HighlightImageView(img, Color.argb(200, 42, 18, 191));

	}

}
